<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:01
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\business;


use app\common\lib\Status;
use app\common\validate\Theatre as ValObj;
use app\common\model\mysql\Theatre as ModelObj;
use think\Exception;
use think\facade\Log;

class Theatre extends Base
{
    /**
     * @var ValObj
     */
    protected ValObj $valObj;
    /**
     * @var ModelObj
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }

    public function add(array $data = [])
    {
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        if ($data['start_time'] > $data['end_time']) {
            throw new Exception("开始时间不能大于结束时间");
        }
        return parent::add($data); // TODO: Change the autogenerated stub
    }

    /**
     * @param array $where
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getTheatreByVenueId(array $where = [], string $field = '*'): array
    {
        try {
            $theatre = $this->modelObj->getTheatreByVenueId($where, $field);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $theatre;
    }

    public function getTheatreByList(int $theatre_id = 0)
    {
        //获取id
        try {
            $theatre = $this->getBaseById($theatre_id, '*,start_time as GetSTime,end_time as GetETime,start_time as StartDateA,start_time as StartDayText');
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if (!$theatre) {
            throw new Exception("订单不存在");
        }
        try {
            $seat_detail = (new SeatDetail())->getSeatDetailByIndex($theatre['seat_id']);
        } catch (\Exception $e) {
            throw new Exception($e->getMessage());
        }
        //查询已选的座位   演出id
        try {
            $detail = (new \app\common\model\mysql\OrderDetail())->where('theatre_id', $theatre['id'])
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            throw new Exception("内部错误");
        }
        if (!empty($detail)) {
            $detail_ids = array_column($detail, 'seat_id');
            foreach ($seat_detail as $key => $value) {
                if (in_array($value['SeatCode'], $detail_ids)) {
                    $seat_detail[$key]['Status'] = 2;
                }
            }
        }

        $theatre['seat_detail'] = $seat_detail;
        //获取观影厅
        try {
            $seat = (new \app\common\model\mysql\Seat())->getBaseById((int)$theatre['seat_id']);
        } catch (\Exception $e) {
            throw new Exception("查询错误");
        }
        $theatre['seat'] = $seat;
        return $theatre;
    }

    public function getTheatreByCount(array $where = [])
    {
        try {
            $result = $this->modelObj->field("id,count(*) as count")
                ->where($where)
                ->where('status',Status::mysql_normal)
                ->find();
        }catch (\Exception $e){
            throw new Exception("查询失败");
        }
        if(!$result){
            return 0;
        }
        return $result['count'];
    }
}
